package com.anlu.concurrent.ch1.syn;

/**
 * <p>
 * Description:TODO 详细描述
 * <p>
 * Copyright: 天津联想协同科技有限公司
 * <p>
 *
 * @author anlu
 * @date 2021/9/3 16:50
 */
public class SynTest {
    //初始100000
    private volatile int age = 100000;

    public void setAge() {
        age = age+20;
    }

    private static class TestThread extends Thread{
        private SynTest synTest;

        public TestThread(SynTest synTest,String name) {
            super(name);
            this.synTest = synTest;
        }

        @Override
        public void run() {
            for(int i=0;i<100000;i++) {//递增100000
                synTest.test();
            }
            System.out.println(Thread.currentThread().getName()
                    +"haha age =  "+synTest.getAge());
        }
    }
    public synchronized void test() {
        age++;
        test2();
    }

    public synchronized void test2() {
        {
            age--;
        }
    }

    public int getAge() {
        return age;
    }

    public static void main(String[] args) {
        SynTest synTest = new SynTest();
        Thread endThread = new TestThread(synTest,"endThread");
        endThread.start();
        //递减100000
        for(int i=0;i<100000;i++) {
            synTest.test2();
        }
        System.out.println(Thread.currentThread().getName()
                +" age =  "+synTest.getAge());
    }

}
